from neopixel import NeoPixel


class Led:
    def __init__(self, pin):
        self.pin = pin
        self.np = NeoPixel(self.pin, 1)
        self.state = False  # LED is initially off

    def on(self, r, g, b, brightness=1.0):
        # Set the LED color with brightness adjustment
        print("LED on")
        brightness = max(0.0, min(1.0, brightness))
        adjusted_r = int(r * brightness)
        adjusted_g = int(g * brightness)
        adjusted_b = int(b * brightness)
        # 应用到 LED
        self.np[0] = (adjusted_r, adjusted_g, adjusted_b)
        self.np.write()
        self.state = True

    def off(self):
        # Turn off the LED
        print("LED off")
        # Here you would add the code to actually turn off the LED
        self.np[0] = (0, 0, 0)
        self.np.write()
        self.state = False

    def toggle(self, r, g, b, brightness=1.0):
        # Toggle the LED state
        print("LED toggle")
        if self.state:
            self.off()
        else:
            self.on(r, g, b, brightness)